var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var helperOpen = require("helper/open");
var qqHttp = require("helper/http");
var httpSilence = require("helper/httpSilence");

var userUrl = require("url/user");
var userAddUrl = userUrl.url.bank.add;
var dictUrl = userUrl.url.dict.list;

var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
//andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
labelNext.visible = true;
headerLeft.on("touch", function() {
	do_App.closePage()
});
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "我的银行卡",
	"labelNext" : "确定"
})
header.refreshData();

// 页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
});
var type=1;
var do_TextField_1=ui("do_TextField_1");
var do_TextField_2=ui("do_TextField_2");
var do_TextField_3=ui("do_TextField_3");
headerRight.on("touch",'',3000,function(data,e){
	do_Page.hideKeyboard();
    if(null==do_TextField_1.text||""==do_TextField_1.text.trim()){
    	nf.toast("请输入支付宝或银行卡账户。");  
        return;
    }
    var retPhone = /^1\d{10}$/;
    var retEmall = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
    if(type==1){
	    if(!retEmall.test(do_TextField_1.text)&&!retPhone.test(do_TextField_1.text)){
    		nf.toast("请正确输入支付宝账号，只能是邮箱或者手机号码。");  
	        return;
	    }
    }else{
    	if(isNaN(do_TextField_1.text)||do_TextField_1.text.length < 16||do_TextField_1.text.length > 20||do_TextField_1.text<0){
    		nf.toast("请正确输入银行卡账号。");  
	        return;
	    }
    }
    if(null==do_TextField_2.text||""==do_TextField_2.text.trim()){
    	nf.toast("请输入真实姓名。");  
        return;
    }
    if(type==2){
    	if(null==ui("do_Label_8").text||""==ui("do_Label_8").text.trim()){
        	nf.toast("请选择银行。");  
            return;
        }
    }
    var params = {
			"type" : type,
			"accountNo":do_TextField_1.text,
			"userReal":do_TextField_2.text,
			"bankName":ui("do_Label_8").text,
			"subbranch":do_TextField_3.text
		};
		qqHttp.post(userAddUrl, params, function(data) {
			nf.toast("添加成功");
			do_App.closePage({fromPage:'bank_add'});
		})
})


var do_ALayout_3=ui("do_ALayout_3");
var do_ALayout_4=ui("do_ALayout_4");
var do_ALayout_12=ui("do_ALayout_12");
var do_ALayout_13=ui("do_ALayout_13");
var do_ALayout_18=ui("do_ALayout_18");

var do_ImageView_1=ui("do_ImageView_1");
var do_ImageView_2=ui("do_ImageView_2");
var radioItem=[do_ImageView_1,do_ImageView_2]

do_ALayout_3.on("touch",function(data,e){
	checkRadio(0);
	do_ALayout_13.visible=false;
	do_ALayout_18.visible=false;
	type=1;
	ui("layoutMain").redraw();
})
do_ALayout_4.on("touch",function(data,e){
	checkRadio(1);
	do_ALayout_13.visible=true;
	do_ALayout_18.visible=true;
	type=2;
	ui("layoutMain").redraw();
})
function checkRadio(index){
	for (var i = 0; i < radioItem.length; i++) {
		if(i==index){
			radioItem[i].source="source://image/common/radiocheck.png";
		}else{
			radioItem[i].source="source://image/common/radio.png";
		}
	}
}
var addPicker = ui("do_ALayout_root").add("picker",
		"source://view/reuse/form/oneCategory.ui", 0, 0);
do_ALayout_18.on("touch", "", 500, function() {
	do_Page.hideKeyboard();
	var ptitle = {
		"title" : "选择银行",
		"id" : "cId"
	}
	httpSilence.post(dictUrl, {type:"user_bank_name"}, function(data) {
		var pcount = [ ptitle, data.list];
		do_Page.fire("oneCategoryList", pcount);
		do_Page.fire("oneCategory", "true");
	});
});
do_Page.on("refreshOneCategory", function(data) {
	if ("cId" == data[1]) {
		ui("do_Label_8").text = data[0].onename;
	}
});


